package vn.edu.nuce.bean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;

import vn.edu.nuce.model.LichDangKy;
import vn.edu.nuce.service.LichDangKyService;
import vn.edu.nuce.utility.Utility;

@ManagedBean(name = "quanLyBean")
@ViewScoped
public class QuanLyBean {
	@ManagedProperty(value = "#{LichDangKyService}")
	LichDangKyService lichDangKyService;

	private Date date;
	private Date date1;
	private Date fromDate;
	private Date toDate;
	private String idHuy;
	private String lyDo;
	private String ngayHienThi;
	List<LichDangKy> lichDangKyList;
	List<LichDangKy> lichDangKyTrungList;

	public QuanLyBean() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		try {
			Date dateWithoutTime = sdf.parse(sdf.format(new Date()));
			setDate(dateWithoutTime);
			setDate1(dateWithoutTime);
		} catch (ParseException e) {
			setDate(new Date());
			setDate1(new Date());
			e.printStackTrace();
		}
		setFromDate();
	}

	public void dongY(String id) {
		LichDangKy lichDangKy = getLichDangKyService().getLichDangKyById(id);
		lichDangKy.setStatus(1);
		updateList(lichDangKy);
		getLichDangKyService().updateLichDangKy(lichDangKy);
		lichDangKyTrungList = getLichDangKyService().hienThiDangKyTrungTheoNgay(lichDangKy.getNgayDat());
		for (LichDangKy itemTrung:lichDangKyTrungList){
			if (!itemTrung.getId().equals(lichDangKy.getId())){
				itemTrung.setStatus(-2);//khong the dong y
				updateList(itemTrung);
				getLichDangKyService().updateLichDangKy(itemTrung);
			}
		}
	}
	private void updateList(LichDangKy lichDangKy){
		int i = 0;
		for (LichDangKy item : lichDangKyList) {
			if (item.getId().equals(lichDangKy.getId())) {
				lichDangKyList.set(i, lichDangKy);
				break;
			}
			i++;
		}
	}

	public void tuChoi() {
		LichDangKy lichDangKy = getLichDangKyService().getLichDangKyById(
				getIdHuy());
		lichDangKy.setStatus(-1);
		lichDangKy.setLyDoHuy(getLyDo());
		int i = 0;
		for (LichDangKy item : lichDangKyList) {
			if (item.getId().equals(lichDangKy.getId())) {
				lichDangKyList.remove(i);
				break;
			}
			i++;
		}
		getLichDangKyService().updateLichDangKy(lichDangKy);
	}
	
	public boolean disabledTuChoi(int status){
		return status==1;
	}
	public boolean disabledDongY(int status){
		return status==1||status==-2;
	}

	public void hienThiDangKyTrungTheoNgay() {
		lichDangKyList = getLichDangKyService().hienThiDangKyTrungTheoNgay(
				getDate1());
	}
	public void hienThiDangKyTrungTheoTuan() {
		lichDangKyList = getLichDangKyService().hienThiDangKyTrungTheoTuan(
				fromDate,toDate);
	}

	public void hienThiDayDu() {
		lichDangKyList = getLichDangKyService().getLichDangKyByDate(getDate());
	}
	
	private void setFromDate(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		fromDate = cal.getTime();		
		cal.add(Calendar.DATE, 6);
		toDate = cal.getTime();
		ngayHienThi = Utility.formatDate(fromDate) + " - "
				+ Utility.formatDate(toDate);
	}

	private void getListTheoTuan() {
		setFromDate();
		lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndChuaHuy(
				fromDate, toDate);
	}
	private void getListTheoNgay(){
		ngayHienThi = Utility.formatDate(date1);
		lichDangKyList = getLichDangKyService().getLichDangKyByDateAndChuaHuy(date1);
	}

	public void handleDateSelect(SelectEvent event) {
		date = (Date) event.getObject();
		getListTheoTuan();		
	}
	public void handleDate1Select(SelectEvent event) {
		date1 = (Date) event.getObject();
		getListTheoNgay();		
	}
	public void onTabChange(TabChangeEvent event) {
		if (event.getTab().getId().equals("tab1")) {
			getListTheoTuan();
		} else if (event.getTab().getId().equals("tab2")) {
			getListTheoNgay();
		} 
	}

	public List<LichDangKy> getLichDangKyList() {
		if (lichDangKyList == null) {
			lichDangKyList = getLichDangKyService()
					.getLichDangKyBy2DateAndChuaHuy(fromDate, toDate);
		}
		return lichDangKyList;
	}

	public void setLichDangKyList(List<LichDangKy> lichDangKyList) {
		this.lichDangKyList = lichDangKyList;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public LichDangKyService getLichDangKyService() {
		return lichDangKyService;
	}

	public void setLichDangKyService(LichDangKyService lichDangKyService) {
		this.lichDangKyService = lichDangKyService;
	}

	public String getIdHuy() {
		return idHuy;
	}

	public void setIdHuy(String idHuy) {
		this.idHuy = idHuy;
	}

	public String getLyDo() {
		return lyDo;
	}

	public void setLyDo(String lyDo) {
		this.lyDo = lyDo;
	}

	public Date getFromDate() {
		return fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public Date getToDate() {
		return toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public String getNgayHienThi() {
		return ngayHienThi;
	}

	public void setNgayHienThi(String ngayHienThi) {
		this.ngayHienThi = ngayHienThi;
	}

	public Date getDate1() {
		return date1;
	}

	public void setDate1(Date date1) {
		this.date1 = date1;
	}

}
